import type { IConfig } from '../config/types';

export const setConfig = (config: IConfig) => ((window as any).IConfig = config);

export const getConfig = (): IConfig => {
  try {
    return JSON.parse(JSON.stringify((window as any).IConfig));
  } catch (e) {
    console.error('getIConfig error: ', e);
  }
  return {} as any;
};

export const svgToImage = (svgStr: string) => {
  return new Promise<HTMLImageElement>((resolve, reject) => {
    const img = new Image();
    img.onload = () => resolve(img);
    img.onerror = reject;
    img.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgStr)}`;
  });
};

export const jsonParse = <T>(str: string): T | undefined => {
  try {
    return JSON.parse(str);
  } catch (e) {
    return;
  }
};

export const sleep = async (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
